<?php

namespace Home\Controller;

use Think\Controller;
use Think\Log;

import("Home.Library.QueryList.QueryList");

class Youzanwm
{
    private $dmid = "youzanxjk"; // "xbbxjkd";
    private $appId = '0e0e9ae485e8de7285';
    private $appSecret = 'd127481accdc062bbc8a10ab9fa926b8';

    private $dbDingdan;//=D("dingdan");
    private $dbSubDingdan;//= D("subdingdan");
    static public $mapId = array();


    protected function _initialize()
    {

        $this->dbDingdan = D("dingdan");
        $this->dbSubDingdan = D("subdingdan");
        //获取一下最近的1000单id,避免冲突添加


//        echo "count=".count(Youzanwm::$mapId);

        $map["sid"] = $this->dmid;
        $list = $this->dbDingdan->field('ddid')->where($map)->order('id desc')->limit(1000)->select();

        $clist = count($list);

        for ($i = 0; $i < $clist; $i++) {

            $ddid = $list[$i]["ddid"];


            array_push(Youzanwm::$mapId, $ddid);

        }

//        if(in_array("E20160227150019024543695",Youzanwm::$mapId))
//        {
//            echo "exist";
//        }else
//        {
//
//            echo "not exist";
//        }


//       var_dump(Youzanwm::$mapId);


        Log::record(" _initialize", "DEBUG");

    }


    public function index()
    {
        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }

    public function deal()
    {
        Log::record(" deal", "DEBUG");
//        echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";


        $this->_initialize();


        $statusArray = array("WAIT_SELLER_SEND_GOODS", "WAIT_BUYER_CONFIRM_GOODS", "TRADE_BUYER_SIGNED");
        $count = count($statusArray);

        $days = -9;

        for ($i = 0; $i < $count; $i++) {
            $status = $statusArray[$i];

            $pageTotal = $this->dealpage(1, $status, $days);

            for ($k = 2; $k <= $pageTotal; $k++) {
                $this->dealpage($k, $status, $days);


                Log::record("page=" . $pageTotal . " dealpage=" . $k, "DEBUG");

            }

        }


//        $pageTotal = $this->dealpage(1);
//
//        for ($i = 2; $i <= $pageTotal; $i++) {
//            $this->dealpage($i);
//
//
//            Log::record(" dealpage=" . $i, "DEBUG");
//        }

        return;


    }

    public function dealTrades($trades)
    {
        $traCount = count($trades);

//		echo "traCount=" . $traCount . "</br>";
        Log::record(" traCount=" . $traCount, "DEBUG");


        $k = 0;

        for ($i = 0; $i < $traCount; $i++) {
            $trade = $trades [$i];

            $type = $trade ["type"];

            if ($type == 'QRCODE') {

                // Method::showpair($trade,0);
                continue;
            }

            // Method::showpair($trade,0);
            $subMenuData = array();

            $data = $this->dealtrade($trade, $subMenuData);


            try {

                if (!in_array($data["ddid"], Youzanwm::$mapId)) {

                    Log::record("ADD DINGDAN" . $data["ddid"], "DEBUG");

                    $this->dbDingdan->add($data);

                }


//               $map=array();
//                $map["ddid"]=$data["ddid"];
//
//                $this->dbDingdan->where($map)->data($data)->save();


            } catch (\Think\Exception $e) {

                Log::record($e->getMessage(), "ERR");
            }

            $count = count($subMenuData);
            for ($s = 0; $s < $count; $s++) {
                try {

                    $ddid = $subMenuData[$s]["ddid"];
                    Log::record("subddid=" . $ddid, "DEBUG");

                    if (!in_array($ddid, Youzanwm::$mapId)) {

                        Log::record("ADD SUB" . $ddid, "DEBUG");
                        $this->dbSubDingdan->add($subMenuData[$s]);

                    }


                } catch (\Think\Exception $e) {
                    Log::record($e->getMessage(), "ERR");
                }
            }

//
//			$k = $k + 1;
//
//			 if($k==10)
//			{
//				break;
//			}
        }
    }

    /*
     * [ 2016-02-26T07:50:30+08:00 ] ::1 /tpxbb/wm/yzdeal
     * DEBUG: key=num value=2
     * DEBUG: key=goods_kind value=2
     * DEBUG: key=num_iid value=91802489
     * DEBUG: key=price value=99.00
     * DEBUG: key=pic_path value=https://dn-kdt-img.qbox.me/upload_files/2015/07/22/FnuK8U3WmquXzVposRasK9Ns7xQd.jpg
     * DEBUG: key=pic_thumb_path value=https://dn-kdt-img.qbox.me/upload_files/2015/07/22/FnuK8U3WmquXzVposRasK9Ns7xQd.jpg?imageView2/2/w/200/h/0/q/75/format/jpg
     * DEBUG: key=title value=十三香1斤装（江苏经典小龙虾）
     * DEBUG: key=type value=FIXED
     * DEBUG: key=discount_fee value=20.00
     * DEBUG: key=status value=TRADE_BUYER_SIGNED
     * DEBUG: key=status_str value=已完成
     * DEBUG: key=refund_state value=NO_REFUND
     * DEBUG: key=shipping_type value=express
     * DEBUG: key=post_fee value=20.00
     * DEBUG: key=total_fee value=117.00
     * DEBUG: key=refunded_fee value=0.00
     * DEBUG: key=payment value=137.00
     * DEBUG: key=created value=2016-02-20 18:53:07
     * DEBUG: key=update_time value=2016-02-21 09:27:09
     * DEBUG: key=pay_time value=2016-02-20 18:55:59
     * DEBUG: key=pay_type value=WEIXIN
     * DEBUG: key=consign_time value=2016-02-20 19:26:14
     * DEBUG: key=sign_time value=2016-02-21 09:27:09
     * DEBUG: key=buyer_area value=北京市北京市
     * DEBUG: key=seller_flag value=0
     * DEBUG: key=buyer_message value=
     * DEBUG: key=orders value=Array
     * DEBUG: key=fetch_detail value=
     * DEBUG: key=coupon_details value=Array
     * DEBUG: key=promotion_details value=Array
     * DEBUG: key=adjust_fee value=0.00
     * DEBUG: key=sub_trades value=Array
     * DEBUG: key=weixin_user_id value=648881032
     * DEBUG: key=button_list value=Array
     * DEBUG: key=feedback_num value=0
     * DEBUG: key=trade_memo value=
     * DEBUG: key=fans_info value=Array
     * DEBUG: key=buy_way_str value=
     * DEBUG: key=pf_buy_way_str value=运费到付
     * DEBUG: key=send_num value=2
     * DEBUG: key=user_id value=14729083
     * DEBUG: key=kind value=2
     * DEBUG: key=buyer_nick value=❥ღ༄྄ེིོུ蜜蜜ཉ྄ིུ♚
     * DEBUG: key=tid value=E20160220185307089170058
     * DEBUG: key=buyer_type value=1
     * DEBUG: key=buyer_id value=648881032
     * DEBUG: key=receiver_city value=北京市
     * DEBUG: key=receiver_district value=丰台区
     * DEBUG: key=receiver_name value=蜜蜜
     * DEBUG: key=receiver_state value=北京市
     * DEBUG: key=receiver_address value=政馨家园二区二号楼1单元1506（电梯11层）
     * DEBUG: key=receiver_zip value=
     * DEBUG: key=receiver_mobile value=18611139925
     * DEBUG: key=feedback value=0
     * DEBUG: key=outer_tid value=1009810141201602203413248022
     *
     *
     *
     *  3key=buyer_messages    3value=Array     count=1
DEBUG: 4key=0    4value=Array     count=2
DEBUG: 5key=title    5value=用餐时间     count=1
DEBUG: 5key=content    5value=今晚六点     count=1
     *
     *
     * DEBUG: 0key=fans_info    0value=Array     count=4
DEBUG: 1key=fans_nickname    1value=阿明     count=1
     *
     */
    // id,ddid,name,sex,mobile,ltrim(rtrim(address)) as address,menu,ordertime,price,userpay,platpay,bosspay,income,status,invoice,shipping,memo
    function dealTrade($trade, &$subMenuData)
    {


        $data = array();
        $data ["ddid"] = $trade ["tid"];
        $data ["sid"] = $this->dmid;

        $data ["name"] = $trade ["receiver_name"];
        if (strlen($data ["name"]) == 0) {
            $fetch = $trade ["fetch_detail"];
            if (count($fetch) > 0) {

                $data ["name"] = $trade ["fetch_detail"]["fetcher_name"];

            }

            if (strlen($data ["name"]) == 0) {

                $data ["name"] = $trade ["buyer_nick"];

                if (strlen($data ["name"]) == 0) {
                    $data ["name"] = $trade ["fans_info"]["fans_nickname"];
                }


            }


//            Method::showpair($trade,0);

        }

        $data ["mobile"] = $trade ["receiver_mobile"];
        $data ["address"] = $trade ["receiver_address"];

        if (strlen($data ["address"]) == 0) {
            $data ["address"] = "到店自提";

        }


        $data ["memo"] = $trade ["trade_memo"] . "  " . $trade ["buyer_messages"];
        $data ["ordertime"] = $trade ["pay_time"];
        $data ["price"] = $trade ["total_fee"];
        $data ["userpay"] = $trade ["payment"];
        $data ["shipping"] = $trade ["post_fee"];
        $data ["income"] = $trade ["payment"];
        $data ["status"] = 1;
        $data ["platpay"] = 0;
        if (count($trade ["coupon_details"]) > 0) {
            $data ["bosspay"] = $trade ["coupon_details"][0]["discount_fee"];

        } else {
            $data ["bosspay"] = 0;
        }


        $data ["sub_menu"] = 1;


        if (stripos($data ["name"], "先生") > 0) {

            $data ["sex"] = 1;
        } else if (stripos($data ["name"], "女士") > 0) {
            $data ["sex"] = 0;

        } else {
            $data ["sex"] = 2;

        }


        // $data["name"]=$trade["receiver_name"];

        // sex ,menu,invoce,
        $orders = $trade ["orders"]; // = 0;
        $ordrCount = count($orders);

        $menu = "";

        for ($i = 0; $i < $ordrCount; $i++) {
            $sub = array();
            $sub ["ddid"] = $data ["ddid"];
            $sub ["subid"] = $orders [$i] ["oid"];
            $sub ["name"] = $orders [$i] ["title"];
            $sub ["current_price"] = $orders [$i] ["price"];
            $sub ["origin_price"] = $orders [$i] ["price"];
            $sub ["number"] = $orders [$i] ["num"];
            $sub ["total_price"] = $orders [$i] ["total_fee"];

            $msg = $orders [$i] ["buyer_messages"];
            if (count($msg) > 0) {


                $data ["memo"] = $data ["memo"] . $msg[0]["title"] . $msg[0]["content"];
            }


            $menu = $menu . $sub ["name"] . "&nbsp;" . $sub ["current_price"] . "&nbsp;" . $sub ["number"] . "&nbsp;" . $sub ["total_price"] . "</br>";

            // var_dump ( $sub );
            // echo "</br></br>";
            array_push($subMenuData, $sub);


        }
        $menu = $menu . "   运费:" . $data ["shipping"] . "   优惠:" . $data ["bosspay"] . "   总计:" . $data ["income"];
        $data ["menu"] = $menu;

        return $data;

//		 var_dump ( $data );
    }

//    public function dealpage($pageNo)
    public function dealpage($pageNo, $status, $lastdays)
    {
        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        // echo "youzan";
        echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";

        $appId = $this->appId;
        $appSecret = $this->appSecret;

        // $client = new KdtApiClient ( $appId, $appSecret );

        $method = "kdt.trades.sold.get";
        $v = "1.0";
        date_default_timezone_set('PRC');
        $timestamp = date('Y-m-d H:i:s', time());
//        $start_created = "2016-02-28 01:00:00";
        $start_created = date('Y-m-d h:i:s', strtotime($lastdays . ' day'));
        $pageSize = 100;

        $page_no = $pageNo;


        //       WAIT_SELLER_SEND_GOODS
//        $status = "WAIT_BUYER_CONFIRM_GOODS";
//        $status = "TRADE_BUYER_SIGNED";
        // $status="WAIT_SELLER_SEND_GOODS";
        // $status="WAIT_SELLER_SEND_GOODS";

        $params = array(
            "app_id" => $appId,
            "method" => $method,
            "page_no" => $page_no,
            "page_size" => $pageSize,

            "status" => $status,
            "timestamp" => $timestamp,
            "start_created" => $start_created,
            "v" => $v
        );

        $preMd5 = $appSecret . "app_id" . $appId . "method" . $method . "page_no" . $page_no . "page_size" . $pageSize . "start_created" . $start_created . "status" . $status . "timestamp" . $timestamp . "v" . $v . $appSecret;

        $sign = md5($preMd5);

//		echo "</br>";

        $url = "https://open.koudaitong.com/api/entry?timestamp=";
        $url = $url . $timestamp . "&sign=" . $sign . "&v=1.0&app_id=" . $appId . "&method=" . $method . "&start_created=" . $start_created . "&page_size=" . $pageSize . "&page_no=" . $page_no . "&status=" . $status;


        Log::record($url, "DEBUG");
//		echo $url, "</br>";
        // echo $url;

        $url = $this->gstr($url);
        $contents = file_get_contents($url);
        // 如果出现中文乱码使用下面代码
        // $getcontent = iconv("gb2312", "utf-8",$contents);
        $dd = json_decode($contents, true);

        $total = $dd ["response"] ["total_results"];
        $trades = $dd ["response"] ["trades"];
        $ddcount = count($trades);

        if ($ddcount > 0) {
            $this->dealTrades($trades);

        }


//        var_dump ( $dd );

//         Method::showpair($trades,0);

        return $total;
    }

    /*
     *
     * ["type"]=>
     * string(6) "QRCODE"
     * ["discount_fee"]=>
     * string(4) "0.00"
     * ["status"]=>
     * string(18) "TRADE_BUYER_SIGNED"
     * ["status_str"]=>
     * string(9) "已完成"
     * ["shipping_type"]=>
     * string(7) "express"
     * ["post_fee"]=>
     * string(4) "0.00"
     * ["total_fee"]=>
     * string(6) "249.00"
     * ["refunded_fee"]=>
     * string(4) "0.00"
     * ["payment"]=>
     * string(6) "249.00"
     * ["created"]=>
     * string(19) "2016-02-24 22:14:01"
     * ["update_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["pay_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["pay_type"]=>
     * string(6) "WEIXIN"
     * ["consign_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["sign_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * * ["buyer_nick"]=>
     * string(6) "匿名"
     * ["tid"]=>
     * string(24) "E20160224221401024260283"
     * ["buyer_type"]=>
     * string(1) "1"
     * ["buyer_id"]=>
     * string(10) "1030469698"
     * ["receiver_city"]=>
     * string(0) ""
     * ["receiver_district"]=>
     * string(0) ""
     * ["receiver_name"]=>
     * string(0) ""
     * ["receiver_state"]=>
     * string(0) ""
     * ["receiver_address"]=>
     * string(21) "面对面扫码收款"
     *
     * ["receiver_mobile"]=>
     * string(0) ""
     *
     * ["outer_tid"]=>
     * string(28) "1010200141201602243509966343"
     * }
     *
     *
     *
     *
     * array(55) {
     * ["num"]=>
     * int(1)
     * ["goods_kind"]=>
     * int(1)
     * ["num_iid"]=>
     * string(7) "2739952"
     * ["price"]=>
     * string(6) "249.00"
     * ["pic_path"]=>
     * string(63) "https://dn-kdt-img.qbox.me/public_files/image/cashier_order.jpg"
     * ["pic_thumb_path"]=>
     * string(102) "https://dn-kdt-img.qbox.me/public_files/image/cashier_order.jpg?imageView2/2/w/200/h/0/q/75/format/jpg"
     * ["title"]=>
     * string(16) "虾Baby的收款"
     * ["type"]=>
     * string(6) "QRCODE"
     * ["discount_fee"]=>
     * string(4) "0.00"
     * ["status"]=>
     * string(18) "TRADE_BUYER_SIGNED"
     * ["status_str"]=>
     * string(9) "已完成"
     * ["refund_state"]=>
     * string(9) "NO_REFUND"
     * ["shipping_type"]=>
     * string(7) "express"
     * ["post_fee"]=>
     * string(4) "0.00"
     * ["total_fee"]=>
     * string(6) "249.00"
     * ["refunded_fee"]=>
     * string(4) "0.00"
     * ["payment"]=>
     * string(6) "249.00"
     * ["created"]=>
     * string(19) "2016-02-24 22:14:01"
     * ["update_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["pay_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["pay_type"]=>
     * string(6) "WEIXIN"
     * ["consign_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["sign_time"]=>
     * string(19) "2016-02-24 22:14:07"
     * ["buyer_area"]=>
     * string(0) ""
     * ["seller_flag"]=>
     * int(0)
     * ["buyer_message"]=>
     * string(0) ""
     * ["orders"]=>
     * array(1) {
     * [0]=>
     * array(25) {
     * ["oid"]=>
     * int(17852806)
     * ["outer_sku_id"]=>
     * string(0) ""
     * ["outer_item_id"]=>
     * string(0) ""
     * ["title"]=>
     * string(16) "虾Baby的收款"
     * ["seller_nick"]=>
     * string(7) "虾Baby"
     * ["fenxiao_price"]=>
     * string(4) "0.00"
     * ["fenxiao_payment"]=>
     * string(4) "0.00"
     * ["price"]=>
     * string(6) "249.00"
     * ["total_fee"]=>
     * string(6) "249.00"
     * ["payment"]=>
     * string(6) "249.00"
     * ["discount_fee"]=>
     * string(4) "0.00"
     * ["sku_id"]=>
     * int(0)
     * ["sku_unique_code"]=>
     * string(0) ""
     * ["sku_properties_name"]=>
     * string(0) ""
     * ["pic_path"]=>
     * string(63) "https://dn-kdt-img.qbox.me/public_files/image/cashier_order.jpg"
     * ["pic_thumb_path"]=>
     * string(102) "https://dn-kdt-img.qbox.me/public_files/image/cashier_order.jpg?imageView2/2/w/200/h/0/q/75/format/jpg"
     * ["item_type"]=>
     * int(30)
     * ["buyer_messages"]=>
     * array(0) {
     * }
     * ["order_promotion_details"]=>
     * array(0) {
     * }
     * ["state_str"]=>
     * string(9) "待发货"
     * ["allow_send"]=>
     * int(1)
     * ["is_send"]=>
     * int(0)
     * ["item_refund_state"]=>
     * string(0) ""
     * ["num_iid"]=>
     * string(7) "2739952"
     * ["num"]=>
     * string(1) "1"
     * }
     * }
     * ["fetch_detail"]=>
     * NULL
     * ["coupon_details"]=>
     * array(0) {
     * }
     * ["promotion_details"]=>
     * array(0) {
     * }
     * ["adjust_fee"]=>
     * string(4) "0.00"
     * ["sub_trades"]=>
     * array(0) {
     * }
     * ["weixin_user_id"]=>
     * string(10) "1030469698"
     * ["button_list"]=>
     * array(1) {
     * [0]=>
     * array(7) {
     * ["tool_icon"]=>
     * string(84) "http://imgqn.koudaitong.com/upload_files/2015/08/28/FpO1UIXyOEZO026tWIgUOm9uZnT2.png"
     * ["tool_title"]=>
     * string(6) "备注"
     * ["tool_value"]=>
     * string(0) ""
     * ["tool_type"]=>
     * string(22) "goto_native:trade_memo"
     * ["tool_parameter"]=>
     * string(2) "{}"
     * ["new_sign"]=>
     * string(1) "0"
     * ["create_time"]=>
     * string(0) ""
     * }
     * }
     * ["feedback_num"]=>
     * int(0)
     * ["trade_memo"]=>
     * string(0) ""
     * ["fans_info"]=>
     * array(4) {
     * ["fans_nickname"]=>
     * string(6) "匿名"
     * ["fans_id"]=>
     * string(10) "1030469698"
     * ["buyer_id"]=>
     * string(9) "123031794"
     * ["fans_type"]=>
     * string(1) "1"
     * }
     * ["buy_way_str"]=>
     * string(0) ""
     * ["pf_buy_way_str"]=>
     * string(12) "运费到付"
     * ["send_num"]=>
     * int(0)
     * ["user_id"]=>
     * string(9) "123031794"
     * ["kind"]=>
     * int(1)
     * ["buyer_nick"]=>
     * string(6) "匿名"
     * ["tid"]=>
     * string(24) "E20160224221401024260283"
     * ["buyer_type"]=>
     * string(1) "1"
     * ["buyer_id"]=>
     * string(10) "1030469698"
     * ["receiver_city"]=>
     * string(0) ""
     * ["receiver_district"]=>
     * string(0) ""
     * ["receiver_name"]=>
     * string(0) ""
     * ["receiver_state"]=>
     * string(0) ""
     * ["receiver_address"]=>
     * string(21) "面对面扫码收款"
     * ["receiver_zip"]=>
     * string(0) ""
     * ["receiver_mobile"]=>
     * string(0) ""
     * ["feedback"]=>
     * int(0)
     * ["outer_tid"]=>
     * string(28) "1010200141201602243509966343"
     * }
     *
     */
    function dealbak()
    {

        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        // echo "youzan";
        echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";

        $appId = '0e0e9ae485e8de7285';
        $appSecret = 'd127481accdc062bbc8a10ab9fa926b8';
        // $client = new KdtApiClient ( $appId, $appSecret );

        $method = "kdt.trades.sold.get";
        $v = "1.0";
        date_default_timezone_set('PRC');
        $timestamp = date('Y-m-d H:i:s', time());

        $params = array(
            "app_id" => $appId,
            "method" => $method,
            "timestamp" => $timestamp,
            "v" => $v
        );

        $preMd5 = $appSecret . "app_id" . $appId . "method" . $method . "timestamp" . $timestamp . "v" . $v . $appSecret;

        $sign = md5($preMd5);

        echo "</br>";
        // echo $preMd5;

        echo '<pre>';

        $url = "https://open.koudaitong.com/api/entry?timestamp=";
        $url = $url . $timestamp . "&sign=" . $sign . "&v=1.0&app_id=" . $appId . "&method=" . $method;

        echo "</br>";
        // echo $url;

        echo "</br>";
        echo "</br>";
        echo "</br>";
        $url = $this->gstr($url);
        $contents = file_get_contents($url);
        // 如果出现中文乱码使用下面代码
        // $getcontent = iconv("gb2312", "utf-8",$contents);
        $dd = json_decode($contents, true);
        $trades = $dd ["response"] ["trades"];
        $ddcount = count($trades);


//        var_dump($trades [0]);
    }

    function gstr($str)
    {
        $encode = mb_detect_encoding($str, array(
            'ASCII',
            'UTF-8',
            'GB2312',
            'GBK'
        ));
        if (!$encode == 'UTF-8') {
            $str = iconv('UTF-8', $encode, $str);
        }
        return $str;
    }
}